from django.http import HttpResponse
import requests
import re


class X23usCom:

    def __init__ (self,key_word):

        self.key_word = key_word
    
    def index(self):
        key_word = self.key_word.encode('gb2312')
        #key_word = urllib.parse.quote(key_word)
        url = 'https://www.x23us.com/modules/article/search.php'
        params = {
            'searchtype':'keywords',
            'searchkey':key_word,
            }
        
        res = requests.get(url,params=params)
        #print(res.history)
        if res.history:
            return {'url':res.url}
        else:        
            return res

    def info(self):
        
        html = self.index().text
        book_name = '<td class="odd"><a href=".*?">(.*?)</a></td>'        
        book_url = '<td class="even"><a href="https://www.x23us.com/html/(.*?)" target="_blank"> .*?</a></td>'
        book_author = '<td class="odd">(\w*?)</td>'
        book_statu = '<td class="even" align="center">(.*?)</td>'
        
        book_name = re.findall(book_name,html)
        #print(len(book_name))
        book_url = re.findall(book_url,html)
        #print(len(book_url))
        book_author = re.findall(book_author,html)
        #print(len(book_author))
        book_statu = re.findall(book_statu,html)
        #print(len(book_statu))
        info_dict = {}
        for i in range(len(book_url)):
            info_dict[book_name[i].replace('<b style="color:red">','').replace('</b>','')] = {'author':book_author[i],'url':book_url[i],'statu':book_statu[i],}
            
        return info_dict
        

        

        

        
if __name__ =='__main__':



    x = index()
























    
